<template>
  <el-form inline="true">
    <el-form-item>
      <selector v-model="params.difficulty" placeholder="请选择题⽬难度" style="width: 200px;">
      </selector>
    </el-form-item>
    <el-form-item>
      <el-input v-model="params.title" placeholder="请您输⼊要搜索的题⽬标题" />
    </el-form-item>
    <el-form-item>
      <el-button @click="onSearch" plain>搜索</el-button>
      <el-button @click="onReset" plain type="info">重置</el-button>
      <el-button @click="onAddQuestion" plain type="primary" :icon="Plus">添加题⽬</el-button>
    </el-form-item>
  </el-form>
  <el-table height="526px" :data="questionList">
    <el-table-column prop="questionId" width="180px" label="题⽬id" />
    <el-table-column prop="title" label="题⽬标题" />
    <el-table-column prop="difficulty" label="题⽬难度" width="90px">
      <template #default="{ row }">
        <div v-if="row.difficulty === 1" style="color:#3EC8FF;">简单</div>
        <div v-if="row.difficulty === 2" style="color:#FE7909;">中等</div>
        <div v-if="row.difficulty === 3" style="color:#FD4C40;">困难</div>
      </template>
    </el-table-column>
    <el-table-column prop="createName" label="创建⼈" width="140px" />
    <el-table-column prop="createTime" label="创建时间" width="180px" />
    <el-table-column label="操作" width="100px" fixed="right">
      <template #default="{ row }">
        <el-button type="text" @click="editQuestionDetail(row.questionId)">编辑
        </el-button>
        <el-button type="text" class="red" @click="deleteQuestionDetail(row.questionId)">删除
        </el-button>
      </template>
    </el-table-column>
  </el-table>
   <el-pagination
      v-model:current-page="params.pageNum"
      v-model:page-size="params.pageSize"
      :size=small
      :page-sizes="[1, 5, 10, 15, 20, 25]"
      layout="total, sizes, prev, pager, next, jumper"
      :total="total"
      @size-change="handleSizeChange" @current-change="handleCurrentChange"
    />
    <question-drawer ref="questionEditRef" @success="onSuccess"></question-drawer>
</template>
<script setup>
import { Plus } from "@element-plus/icons-vue"
import Selector from "@/components/QuestionSelector.vue"
import { reactive, ref } from 'vue'
import { getQuestionListService,delQuestionService } from "@/apis/question"
import QuestionDrawer from "@/components/QuestionDrawer.vue"
import {ElMessageBox,ElMessage} from "element-plus"

const params = reactive({
  title:'',
  difficulty:'',
  pageSize:10,
  pageNum:1
})
const questionList =ref([])
const total = ref(0);

async function getQuestionList() {
  const res = await getQuestionListService(params);
  console.log(res);
  questionList.value=res.rows;
  total.value=res.total;
}
// 进入这个页面的时候自动进行调用
getQuestionList();
function handleSizeChange() {
  params.pageNum=1;
  getQuestionList();
}
function handleCurrentChange() {
  getQuestionList();
}

function onReset() {
  params.title='',
  params.difficulty='',
  params.pageNum=1,
  params.pageSize=10
  getQuestionList();
}
function onSearch() {
  getQuestionList();
}


const  onAddQuestion = () => {
  questionEditRef.value.open();
}

function onSuccess(service) {
  if (service === 'add') {
    params.pageNum = 1
  }
  getQuestionList()
}

const questionEditRef = ref();
function editQuestionDetail(questionId) {
  //这里直接调用抽屉组件的open
  questionEditRef.value.open(questionId);
}

async function deleteQuestionDetail(questionId) {
  await ElMessageBox.confirm(
    '确认删除后，无法恢复数据，请谨慎',
    '温馨提示',
    {
      confirmButtonText: '确认',
      cancelButtonText: '取消',
      type: 'warning',
    }
  )
  await delQuestionService(questionId);
  ElMessage.success("删除成功");
  params.pageNum=1;
  getQuestionList();
}
</script>
